package com.yakaten.logic;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.seasar.extension.jdbc.SqlUpdate;
import org.seasar.framework.beans.util.BeanMap;

import com.yakaten.dto.TaskInfoDto;
import com.yakaten.exception.SystemException;
import com.yakaten.service.JdbcManagerFactory;

/**
 *
 *
 */
public class TaskInfoLogic extends JdbcManagerFactory {

	private static final String TABLE_NAME = "task_info";


	static Logger logger = Logger.getLogger(TaskInfoLogic.class);


	/**
	 *
	 * @return
	 * @throws SystemException
	 */
	public List<TaskInfoDto> getAllTaskList() throws SystemException {
		List<TaskInfoDto> resultList = new ArrayList<TaskInfoDto>();

		List<BeanMap> result = jdbcManager.selectBySql(BeanMap.class,
				"SELECT * FROM " + TABLE_NAME).getResultList();

		for (BeanMap current : result) {
			TaskInfoDto dto = new TaskInfoDto();
			dto.setInfoNo((String) current.get("infono"));
			dto.setInfoName((String) current.get("infoname"));
			dto.setUserName((String) current.get("username"));
			dto.setPassword((String) current
							.get("password"));
			dto.setRunStatus((String) current.get("runstatus"));
			dto.setSleepTime(((BigDecimal) current.get("sleeptime"))
					.longValue());


			resultList.add(dto);
		}
		return resultList;
	}

	/**
	 *
	 * @return
	 * @throws SystemException
	 */
	public TaskInfoDto getTaskInfo(TaskInfoDto taskInfoDto) throws SystemException {
		TaskInfoDto dto = null;

		BeanMap result = jdbcManager.selectBySql(BeanMap.class,
				"SELECT * FROM " + TABLE_NAME + " where infoNo='"+taskInfoDto.getInfoNo()+"'").getSingleResult();

		if(result!=null){
			dto = new TaskInfoDto();
			dto.setInfoNo((String) result.get("infono"));
			dto.setInfoName((String) result.get("infoname"));
			dto.setUserName((String) result.get("username"));
			dto.setPassword((String) result
							.get("password"));
			dto.setRunStatus((String) result.get("runstatus"));
			dto.setSleepTime(((BigDecimal) result.get("sleeptime"))
					.longValue());
		}


		return dto;
	}

	/**
	 *
	 * @return
	 * @throws SystemException
	 */
	public String getNewTaskInfoNo() throws SystemException {
		String newTaskInfoNo = "1";

		BeanMap result = jdbcManager.selectBySql(
				BeanMap.class,
				"SELECT max(cast(infoNo as SIGNED)) +1 infoNo FROM "
						+ TABLE_NAME).getSingleResult();

		if (result != null && result.get("infono") !=null) {
			newTaskInfoNo = ((Long) result.get("infono")).toString();
		}

		return newTaskInfoNo;
	}

	/**
	 *
	 * @param dto
	 * @return
	 * @throws SystemException
	 */
	public boolean addTaskInfo(TaskInfoDto dto) throws SystemException {
		boolean result = false;
		String sql = "INSERT INTO " + TABLE_NAME
				+ " VALUES(?,?,?,?,?,?)";
		SqlUpdate batchUpdate = jdbcManager.updateBySql(sql, String.class,
				String.class, String.class,  String.class,
				String.class, Integer.class);
		batchUpdate.params(dto.getInfoNo(), dto.getInfoName(), dto
				.getUserName(), dto.getPassword(), dto
				.getRunStatus(), dto.getSleepTime());

		try {
			int re = batchUpdate.execute();
			if (re == 1) {
				result = true;
			}
		} catch (Exception e) {
			throw new SystemException(e);
		}

		return result;
	}


	/**
	 *
	 * @param updateDto
	 * @return
	 * @throws SystemException
	 */
	public  boolean updateTaskInfo(TaskInfoDto updateDto)
			throws SystemException {
		boolean result = false;

		String sql = "update " + TABLE_NAME + " set " + "infoName = ? "
				+ ",userName = ? " + ",password = ? "
				+ ",runStatus = ? " + ",sleepTime = ? "
				+ "where infoNo = ?";

		SqlUpdate sqlUpdate = jdbcManager.updateBySql(sql, String.class,
				String.class,  String.class, String.class,
				Integer.class, String.class);

		sqlUpdate.params(updateDto.getInfoName(),updateDto.getUserName(),updateDto.getPassword(),updateDto.getRunStatus(),updateDto.getSleepTime(),updateDto.getInfoNo());

		try {
			int res = sqlUpdate.execute();
			if (res == 1) {
				result = true;
			}

		} catch (Exception e) {
			throw new SystemException(e);
		}

		return result;
	}


	public static void main(String[] args) {
		try {

			TaskInfoLogic logic = new TaskInfoLogic();
			System.out.println(logic.getNewTaskInfoNo());
//			TaskInfoDto dto = new TaskInfoDto();
//			dto.setInfoNo("1");
//			dto.setInfoName("Kakaku");
//			dto.setUserName("kakakuser");
//			dto.setPassword("passwor");
//			dto.setRunStatus("1");
//			dto.setSleepTime(1000);
//			 System.out.println(logic.addTask(dto));
//			;
////			 System.out.println(logic.getNewTaskNo());
			 System.out.println(logic.getAllTaskList().get(0));
//
			TaskInfoDto dto1 = new TaskInfoDto();
			dto1.setInfoNo("1");
			dto1.setInfoName("kakaku1");
			dto1.setUserName("userName1");
			dto1.setPassword("password1");
			dto1.setRunStatus("0");
			dto1.setSleepTime(200);
//
//			 System.out.println(logic.updateTask(dto1));
			System.out.println(logic.getTaskInfo(dto1));
//			 System.out.println(logic.getAllTaskList().get(0));
//			System.out.println(logic.updateTaskStatus("3", 9));

		} catch (SystemException e) {
			e.printStackTrace();
		}
	}
}
